﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Globalization;

public partial class WebUserControl_ControlApprovePromoteBUM : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetClosedMonthAprrove();
                BindGrid();
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    void BindGrid()
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getPromoteBAList]", CommandType.StoredProcedure,
                new SqlParameter("@userName",Session["username"].ToString()));
            this.GridView1.DataSource = db;
            this.GridView1.DataBind();
            
        }
        catch(Exception ex)
        {
            uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
        }
    }
    //protected void CheckAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        CheckBox c = (CheckBox)GridView1.HeaderRow.FindControl("CheckAll");
    //        if (c.Checked)
    //        {
    //            for (int i = 0; i < GridView1.Rows.Count; i++)
    //            {
    //                CheckBox chon;
    //                chon = (CheckBox)GridView1.Rows[i].FindControl("CheckApprove");
    //                chon.Checked = true;
    //            }
    //        }
    //        else
    //        {
    //            for (int i = 0; i < GridView1.Rows.Count; i++)
    //            {
    //                CheckBox chon;
    //                chon = (CheckBox)GridView1.Rows[i].FindControl("CheckApprove");
    //                chon.Checked = false;
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
    //        string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
    //        obj.ErrorLog(ex.Message, method);
    //    }
    //}
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            //hide PrUp
            e.Row.Cells[14].Visible = false;

           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //align center
                e.Row.HorizontalAlign = HorizontalAlign.Center;

                // determine the value of the Approve field
                int Approve = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PrUp"));
                if (Approve == 1)
                {
                    e.Row.BackColor = Color.Aqua;
                }
                int Duyet = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Duyệt"));
                if (Duyet == 1) 
                {
                    e.Row.BackColor = Color.Yellow;
                    e.Row.Cells[0].Controls.Clear();
                }

                //format doanh so
                e.Row.Cells[5].Text = String.Format("{0:0,0}", Convert.ToDouble(e.Row.Cells[5].Text));
                e.Row.Cells[8].Text = String.Format("{0:0,0}", Convert.ToDouble(e.Row.Cells[8].Text));
                e.Row.Cells[11].Text = String.Format("{0:0,0}", Convert.ToDouble(e.Row.Cells[11].Text));

                e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
                e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
                e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Right;


            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.HorizontalAlign = HorizontalAlign.Center;
               
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void btApprove_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            
            CheckBox c;
            string salesp_key = "";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                c = (CheckBox)GridView1.Rows[i].FindControl("CheckApprove");
                if (c.Checked)
                {
                    salesp_key = GridView1.Rows[i].Cells[1].Text;
                    dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_approvePromotedBA", ref error,
                        new SqlParameter("@salesPKey", salesp_key),
                        new SqlParameter("@userName", Session["username"].ToString()));
                }
            }
            BindGrid();
            
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }

    }
    void GetClosedMonthAprrove()
    {
        dt = new ClassExecute.ClassExecute();
        string month = dt.GetDataDepend(str, "K_GetCLosedMonthAprrove", CommandType.StoredProcedure, ref error);
        uscTitleForm1.Messeage = "Thăng giáng cấp cho TVV trong tháng " + month;
    }
}
